package com.shop.dingdongshoppingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.dingdongshoppingapi.dao.ShopCarDao;
import com.shop.dingdongshoppingapi.dto.LikeToCarDto;
import com.shop.dingdongshoppingapi.dto.ShopCarAddDto;
import com.shop.dingdongshoppingapi.entity.Goods;
import com.shop.dingdongshoppingapi.entity.ShopCar;
import com.shop.dingdongshoppingapi.service.intf.ShopCarService;
import com.shop.dingdongshoppingapi.util.StringUtil;
import com.shop.dingdongshoppingapi.vo.R;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ShopCarServiceImpl implements ShopCarService {
	@Resource
	private ShopCarDao dao;

	@Override
	public R checkShopCar(int token) {
		//判断是否登录
		if (token > 0) {
			List<ShopCar> uid = dao.selectList(new QueryWrapper<ShopCar>().eq("uid", token));
			if (uid.size() < 1) {
				return R.ok(null);
			}
			return R.ok(uid);
		}
		return R.fail("请先登录");
	}

	/*
	 * 删除购物车的单个商品*/
	@Override
	public R delShopById(int id) {
		if (id > 0) {
			//此处可以判断当前token是否存在，但是没必要,没写
			if (dao.delete(new QueryWrapper<ShopCar>().eq("id", id)) > 0) {
				return R.ok();
			}
		}
		return R.fail("亲,您的购物车无此商品");
	}

	@Override
	public R fourRandom() {
		int length = dao.goodsLength();
		if (length > 0) {
			ArrayList<Integer> list = new ArrayList<>();
			Random random = new Random();
			int i = random.nextInt(length - 4);
			for (int j = 0; j < 4; j++) {
				list.add(i+1);
				i++;
			}
			ArrayList<Goods> goods = dao.fourRandom(list);
			return R.ok(goods);
		}
		return R.ok("商品列表为空");
	}

	@Override
	public R addLikeToCar(LikeToCarDto likeToCarDto, int token) {
		if (likeToCarDto != null && token > 0){
			QueryWrapper<ShopCar> wrapper = new QueryWrapper<>();
			wrapper.eq("uid", token);
			wrapper.eq("gname", likeToCarDto.getGname());
			if (dao.selectOne(wrapper) == null){
				//购物车不存在此商品
				likeToCarDto.setUid(token);
				likeToCarDto.setGnum(1);
				if (dao.addLikeToCar(likeToCarDto) > 0){
					return R.ok("已添加到购物车");
				}else{
					return R.fail("添加失败");
				}
			}else{
				//购物已有此商品,对此商品数量+1
				if(dao.addOneNum(token,likeToCarDto.getGname()) > 0){
					return R.ok("数量已加1");
				}else{
					return R.fail("数量加1失败");
				}
			}
		}
		return R.fail("该物品信息不存在");
	}

	@Override
	public R updateShopCarNum(ShopCar shopCar) {
		if (shopCar != null) {
			if (dao.updateShopCarNum(shopCar.getGnum(), shopCar.getId()) > 0){
				return R.ok("成功修改订单中商品数量");
			}
			return R.fail("修改订单中商品数量失败");
		}
		return R.fail();
	}
	//添加商品到购物车
	@Override
	public R add(ShopCarAddDto addDto, Integer uid) {
		//校验参数
		if(addDto!=null){
			//准备对象
			System.out.println(addDto);
			ShopCar shopCar=new ShopCar();
			BeanUtils.copyProperties(addDto,shopCar);
			shopCar.setGctime(new Date());
			shopCar.setUid(2);
			//3.添加到数据库
			if(dao.insert(shopCar)>0){
				return R.ok();
			}
		}
		return R.fail();
	}
}
